$(function () {

var user = getCookie("lgc");
if (user){
    (
        async function(){
            let result = await searchShoppingByUser({user});
            console.log(result);
            let {status,list} =result;
            if(status){
                var html = "";
                list.forEach(({ id, goodsId, goodsName, goodsPrice, goodsImg, goodsNameEn,buyNum }) => {
                    html += `
                        <tr class="tbody" data-id="${id}">
                            <td >&emsp;&emsp;<input type="checkbox" name="" id="" class="checkOne">&emsp;&emsp;</td>
                            <td class="goods">
                                <div class="goods-pic">
                                    <img src="https://www.origins.com.cn/${goodsImg}" alt="">
                                </div>
                                <div class="goods-info">
                                    <p class="goods-info1">${goodsNameEn}</p>
                                    <p class="goods-info2">${goodsName}</p>
                                    <a href="javascript:;" class="del-one">移除</a>
                                </div>
                            </td>
                            <td class="price">${goodsPrice}</td>
                            <td class="number">
                                <select class="count">
                                    <option value="${buyNum}">${buyNum}</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4" checked>4</option>
                                    <option value="5">5</option>
                                    <option value="6">6</option>
                                    <option value="7">7</option>
                                    <option value="8">8</option>
                                    <option value="9">9</option>
                                </select>
                            </td>
                            <td class="subTotal">${((goodsPrice * 1) * (buyNum * 1)).toFixed(2)}</td>
                        </tr>
                `
                })
               
                $(".tbody").html(html);

                //全选
                $(".checkALL").click(function () {
                    var status = $(this).prop("checked");

                    $(".checkOne").prop("checked", status);
                    getTotal();
                   // getFreeGift();
                })
                //单选
                //console.log($(".checkOne"));
                $(".checkOne").click(function () {
                    if ($(".checkOne").length > 0 && $(".checkOne:checked").length === $(".checkOne").length) {
                        $(".checkALL").prop("checked", true);
                    } else {
                        $(".checkALL").prop("checked", false);
                    }
                    getTotal();
                   // getFreeGift();
                })
                //更改value值
                $(".count").change( async function () {
                    var id = $(this).parents("tr").attr("data-id");
                    var buyNum = $(this).val() * 1;
                    var result = await updateShoppingCarById({ id, buyNum});
                    var {status} = result;
                    if(status){
                        var num = $(this).val() * 1;
                        //console.log(num);
                        var price = $(this).parents("tr").find(".price").text() * 1;
                        //console.log(price);
                        var subTotal = (price * num).toFixed(2);
                        $(this).parents("tr").find(".subTotal").text(subTotal);
                    }
                    
                    getTotal();
                    //getFreeGift();
                })
                //单删
                $(".del-one").click( async function () {
                    if (confirm("是否残忍的移除该商品？")) {
                        var id = $(this).parents("tr").attr("data-id");
                        console.log(2222, id);
                        var result = await deleteShoppingCarById({ id });
                        console.log(1111111, result);
                        var { status } = result;
                        if (status) {

                            $(this).parents("tr").remove();

                        }
                     }
                   
                    isAllChecked();
                    getTotal();
                    //getFreeGift();
                })
                //多删
                $(".delAll").click( async function () {
                    if (confirm("是否不开心的移除所有商品？")) {
                       
                   

                    var list = $(".checkOne:checked").parents("tr").map(function(){
                        return $(this).attr("data-id")
                    }).get();
                    console.log(list);
                    var str = list.join(",");
                    console.log(str);
                    var result = await deleteShoppingCarById({ id:str });
                    $(".checkOne:checked").parents("tr").remove();
                    }
                    isAllChecked();
                    getTotal();
                    //getFreeGift();
                })

            }
        }
    )()
}
})

function isAllChecked() {
    if ($(".checkOne").length > 0 && $(".checkOne:checked").length === $(".checkOne").length) {
        $(".checkALL").prop("checked", true);
    } else {
        $(".checkALL").prop("checked", false);
    }
}
function getTotal() {
    var sum = 0;
    var allPrice = 0;
    $(".checkOne:checked").each(function () {
        var num = $(this).parents("tr").find(".count").val() * 1;
        var subTotal = $(this).parents("tr").find(".subTotal").text() * 1;
        sum += num;
        allPrice += subTotal;
    })
    $("#selectedTotal").text(sum);
    $("#priceTotal").text(allPrice.toFixed(2));
}

// function getFreeGift() {
//     var sum = 0;
//     $(".checkOne:checked").each(function () {
//         var num = $(this).parents("tr").find(".count").val() * 1;
//         sum += num;
//     })
//     console.log(sum);
//     if(sum>=3){
//         $(".freeGift").css({ display:"block"});
//     } else {
//         $(".freeGift").css({ display: "none "});
//     }
// } 